/************************************************************************
Purpose: 	Create Plant-Period-Date Panel
*************************************************************************/

set more off
clear all
pause on

use "$TRADING_DATA_OUT/record_plant-commodity-date-bid.dta", clear
drop if bid_status == "REJ"

foreach var of varlist bid_qty bid_value trade_qty trade_value{
	bysort gpcb_id commodity date: egen temp = sum(`var') if `var' > 0 & !missing(`var')
	bysort gpcb_id commodity date: egen buy_`var'_daily_sum = mean(temp)
	drop temp
	bysort gpcb_id commodity date: egen temp = sum(`var') if `var' < 0 & !missing(`var')
	bysort gpcb_id commodity date: egen sell_`var'_daily_sum = mean(temp)
	drop temp
}

local sides buy sell
local types bid trade
foreach s of local sides{
	foreach t of local types{
		gen `s'_`t'_price_daily_avg = `s'_`t'_value_daily_sum / `s'_`t'_qty_daily_sum
	}
}

drop bid_id bid_qty bid_value bid_price bid_status trade_qty trade_value trade_price trade_qty_cum permit_holding
duplicates drop

label var buy_bid_qty_daily_sum "Total quantity of purchase bids placed by day (kg)"
label var buy_bid_price_daily_avg "Average price of purchase bids placed by day (Rs/kg) = buy_bid_value_daily_sum / buy_bid_qty_daily_sum"
label var buy_bid_value_daily_sum "Total value of purchase bids placed by day (Rs)"
label var sell_bid_qty_daily_sum "Total quantity of sale bids placed by day (kg)"
label var sell_bid_price_daily_avg "Average price of sale bids placed by day (Rs/kg) = sell_bid_value_daily_sum / sell_bid_qty_daily_sum"
label var sell_bid_value_daily_sum "Total value of sale bids placed by day (Rs)"
label var buy_trade_qty_daily_sum "Total quantity of permits purchased by day (kg)"
label var buy_trade_price_daily_avg "Average price of permits purchased by day (Rs/kg) = buy_trade_value_daily_sum / buy_trade_qty_daily_sum"
label var buy_trade_value_daily_sum "Total value of permits purchased placed by day (Rs)"
label var sell_trade_qty_daily_sum "Total quantity of permits sold by day (kg)"
label var sell_trade_price_daily_avg "Average price of permits sold by day (Rs/kg) = sell_trade_value_daily_sum / sell_trade_qty_daily_sum"
label var sell_trade_value_daily_sum "Total value of permits sold placed by day (Rs)"

order gpcb_id commodity commodity_period date bid_day bid_day_norm ///
	buy_bid_qty_daily_sum buy_bid_price_daily_avg buy_bid_value_daily_sum ///
	sell_bid_qty_daily_sum sell_bid_price_daily_avg sell_bid_value_daily_sum /// 
	buy_trade_qty_daily_sum buy_trade_price_daily_avg buy_trade_value_daily_sum ///
	sell_trade_qty_daily_sum sell_trade_price_daily_avg sell_trade_value_daily_sum 
	
sort gpcb_id commodity_period date

merge m:1 date commodity using "$TRADING_DATA_OUT/index_day-commodity-period.dta"
drop _merge
sort date gpcb_id

merge m:1 gpcb_id using "$TRADING_DATA_OUT/covariates_plant.dta"
drop _merge

rename commodity_period period
sort gpcb_id period date
save "$TRADING_DATA_CLEAN/panel_plant-period-date.dta", replace
